package org.jeecg.modules.extbpm.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bpm.util.BpmUtil;
import org.jeecg.modules.extbpm.process.common.WorkFlowGlobals;
import org.jeecg.modules.extbpm.process.entity.ExtActDesignFlowData;
import org.jeecg.modules.extbpm.process.entity.ExtActProcessForm;
import org.jeecg.modules.extbpm.process.exception.BpmException;
import org.jeecg.modules.extbpm.process.mapper.ExtActDesignFlowDataMapper;
import org.jeecg.modules.extbpm.process.mapper.ExtActProcessFormMapper;
import org.jeecg.modules.extbpm.process.pojo.DesFormFlowDTO;
import org.jeecg.modules.extbpm.process.pojo.DesignFormDataDTO;
import org.jeecg.modules.extbpm.process.service.IExtActDesignFlowDataService;
import org.jeecg.modules.extbpm.process.service.IExtActProcessService;
import org.jeecg.modules.extbpm.util.JSONHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 表单设计器对接流程
 * @Author: jeecg-boot
 * @Date: 2019-06-26
 * @Version: V1.0
 */
@Service("extActDesignFlowDataService")
public class ExtActDesignFlowDataServiceImpl extends ServiceImpl<ExtActDesignFlowDataMapper, ExtActDesignFlowData> implements IExtActDesignFlowDataService {
	@Autowired
	private IExtActProcessService extActProcessService;
	@Autowired
	private ExtActProcessFormMapper extActProcessFormMapper;

	@Override
	public List<DesFormFlowDTO> getDesFormFlows() {
		return this.baseMapper.getDesFormFlows();
	}

	@Override
	public DesignFormDataDTO getDesignFormDataById(String id) {
		return this.baseMapper.getDesignFormDataById(id);
	}

	/**
	 * 存单据并提交流程接口（表单设计器单据）
	 * （暂时没有用）
	 * @param extActDesignFlowData
	 * @param request
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveAndStartDesFormMutilProcess(ExtActDesignFlowData extActDesignFlowData, HttpServletRequest request) {
		//Step.1 保存表单设计器的流程中间表数据
		DesignFormDataDTO designFormDataDTO = this.getDesignFormDataById(extActDesignFlowData.getDesformDataId());
		extActDesignFlowData.setTableName(designFormDataDTO.getOnlineFormCode());
		Map<String, Object> dataForm = new HashMap<String, Object>();
		dataForm = JSONHelper.json2MapNoObj(designFormDataDTO.getDesformDataJson());
		String bizTitle = BpmUtil.getContent(dataForm, extActDesignFlowData.getTitleExp());
		extActDesignFlowData.setBpmTitle(bizTitle);
		extActDesignFlowData.setBpmStatus(WorkFlowGlobals.BPM_BUS_STATUS_1);
		this.save(extActDesignFlowData);

		//Step.2 发起流程
		String flowCode = extActDesignFlowData.getFlowCode();
		String id = extActDesignFlowData.getId();
		String formUrl = "{{DOMAIN_URL}}/desform/detail/" + extActDesignFlowData.getDesformCode() + "/" + extActDesignFlowData.getDesformDataId() + "?token={{TOKEN}}";
		//TODO 移动地址有问题
		String formUrlMobile = "{{DOMAIN_URL}}/desform/detail/" + extActDesignFlowData.getDesformCode() + "/" + extActDesignFlowData.getDesformDataId() + "?token={{TOKEN}}";
		if (oConvertUtils.isEmpty(flowCode)) {
			throw new BpmException("flowCode参数不能为空");
		}
		if (oConvertUtils.isEmpty(id)) {
			throw new BpmException("id参数不能为空");
		}
		if (oConvertUtils.isEmpty(formUrl)) {
			throw new BpmException("formUrl参数不能为空");
		}
		ExtActProcessForm extActProcessForm = null;
		//获取当前发起人
		String username = "";
		try {
			//查询流程业务关联配置
			LambdaQueryWrapper<ExtActProcessForm> queryWrapper = new LambdaQueryWrapper<ExtActProcessForm>();
			queryWrapper.eq(ExtActProcessForm::getRelationCode, flowCode);
			extActProcessForm = extActProcessFormMapper.selectOne(queryWrapper);
			//获取业务数据，并且加载到流程变量中
			if (WorkFlowGlobals.BPM_FORM_TYPE_2.equals(extActProcessForm.getFormType())) {
				dataForm.put(WorkFlowGlobals.BPM_FORM_KEY, WorkFlowGlobals.DESIGN_FORM_DRAFT_TABLE);
			} else {
				throw new BpmException("该请求不支持该业务");
			}
			dataForm.put(WorkFlowGlobals.BPM_DATA_ID, id);
			dataForm.put(WorkFlowGlobals.BPM_FORM_CONTENT_URL, formUrl);
			if (oConvertUtils.isNotEmpty(formUrlMobile)) {
				dataForm.put(WorkFlowGlobals.BPM_FORM_CONTENT_URL_MOBILE, formUrlMobile);
			} else {
				//没有传移动端表单，默认用PC端
				dataForm.put(WorkFlowGlobals.BPM_FORM_CONTENT_URL_MOBILE, formUrl);
			}
			username = JwtUtil.getUserNameByToken(request);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BpmException("获取流程信息异常");
		}
		log.debug("---- 发起流程方法 -- startDesFormMutilProcess----id------" + id);
		log.debug("---- 发起流程方法 -- startDesFormMutilProcess----dataForm------" + dataForm);
		log.debug("---- 发起流程方法 -- startDesFormMutilProcess----extActProcessForm------" + extActProcessForm);
		log.debug("---- 发起流程方法 -- startDesFormMutilProcess----username------" + username);
		//TODO Service 尽量不要调用 service
		extActProcessService.startDesFormMutilProcess(username, id, dataForm, extActProcessForm);
		return extActDesignFlowData.getId();
	}

	@Override
	public List<DesFormFlowDTO> getDesFormFlowsByProcType(String procType) {
		return this.baseMapper.getDesFormFlowsByProcType(procType);
	}

}
